package com.huas.controller;

import com.huas.common.PageResult;
import com.huas.common.Result;
import com.huas.dto.UserPasswordDTO;
import com.huas.entity.User;
import com.huas.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/userManagement")
@Slf4j
public class UserController {
    @Resource
    private UserService userService;
    /**
     * 条件分页列表查询
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('userManagement:select')")
    public Result<PageResult> page(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10")Integer pageSize,
                                   String name ){
        log.info("用户列表查询参数：{},{},{}",page,pageSize,name);
        PageResult pageResult = userService.page(page,pageSize,name);
        return Result.success(pageResult);
    }
    /**
     * 添加用户
     */
    @PostMapping
    @PreAuthorize("hasAuthority('userManagement:insert')")
    public Result insert(@RequestBody User user){
        log.info("添加的用户信息为：{}",user);
        userService.insert(user);
        return Result.success();
    }

    /**
     * 根据用户id查询用户信息
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('userManagement:select')")
    public Result<User> findById(@PathVariable Integer id){
        log.info("要查询的用户id为：{}",id);
        User user = userService.findById(id);
        return Result.success(user);
    }
    /**
     * 修改用户
     */
    @PutMapping
    @PreAuthorize("hasAuthority('userManagement:update')")
    public Result update(@RequestBody User user){
        log.info("要修改的用户信息为：{}",user);
        userService.update(user);
        return Result.success();
    }
    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('userManagement:delete')")
    public Result delete(@PathVariable Integer id){
        log.info("要删除的用户id为：{}",id);
        userService.delete(id);
        return Result.success();
    }
    /**
     * 用户修改密码
     */
    @PutMapping("/updatePassword")
    @PreAuthorize("hasAuthority('userManagement:user:updatePwd')")
    public Result updatePassword(@RequestBody UserPasswordDTO userPasswordDTO){
        userService.updatePassword(userPasswordDTO);
        return Result.success();
    }
}
